package com.vladium.utils;

import java.lang.reflect.Field;

public class ReflectUtils
{
	public static String fieldName( Field field, boolean shortClassNames )
	{
//		return typeName( field.getDeclaringClass(), shortClassNames ).concat( "#" ).concat(
//			field.getName() );
		return field.getName();
	}

	public static String typeName( Class<?> cls, boolean shortClassNames )
	{
		int dims = 0;
		for( ; cls.isArray(); ++dims )
			cls = cls.getComponentType();
		String clsName = cls.getName();
		if( shortClassNames )
		{
			final int lastDot = clsName.lastIndexOf( '.' );
			if( lastDot >= 0 )
				clsName = clsName.substring( lastDot + 1 );
		}
		else if( ObjectProfiler.SHORT_COMMON_TYPE_NAMES )
		{
			if( clsName.startsWith( "java.lang." ) )
				clsName = clsName.substring( 10 );
			else if( clsName.startsWith( "java.util." ) )
				clsName = clsName.substring( 10 );
		}
		for( int i = 0; i < dims; ++i )
			clsName = clsName.concat( "[]" );
		return clsName;
	}
}
